package JAVA300.第十章IO;

import java.io.*;
import java.net.URL;

/**
 * 转化流
 * 以字符流的形式操作字节流
 */

public class IO_Study12_convertaNetStream {
    public static void main(String[] args) {

        bufferEdition();
//        try {
//            InputStream is = new URL("http://www.baidu.com").openStream();//中文乱码
//            InputStreamReader isr = new InputStreamReader(new URL("http://www.baidu.com").openStream(),
//                    "UTF-8");
//
//            int len =-1;
//            while ((len=isr.read())!= -1){
//                System.out.print((char)len);//字节数不够造成乱码
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


    }
    //添加缓冲

    public static void bufferEdition(){
        try {

            BufferedReader reader =
                    new BufferedReader(
                            new InputStreamReader(new URL("http://www.baidu.com").openStream(),
                    "UTF-8")) ;
            BufferedWriter writer =
                    new BufferedWriter(
                            new OutputStreamWriter((new FileOutputStream("baidu.html")),"UTF-8"));

            String msg =null;
            while ((msg=reader.readLine())!= null){
                writer.write(msg);
                writer.newLine();
            }
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

